package com.example.zhpan.banner.net.common;

import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.zhpan.banner.R;
import com.google.gson.JsonParseException;
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
import com.example.zhpan.banner.net.exception.NoDataExceptionException;
import com.example.zhpan.banner.net.exception.ServerResponseException;

import org.json.JSONException;

import java.io.InterruptedIOException;
import java.net.ConnectException;
import java.net.UnknownHostException;
import java.text.ParseException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * Created by zhpan on 2017/4/18.
 */

public abstract class ResponseObserver<T> implements Observer<T> {
  @Override
  public void onSubscribe(Disposable d) {

  }

  @Override
  public void onNext(T response) {
    onSuccess(response);
    onFinish();
  }

  @Override
  public void onError(Throwable e) {
    LogUtils.e("Retrofit", e.getMessage());
    if (e instanceof HttpException) {     //   HTTP错误
      onException(ExceptionReason.BAD_NETWORK);
    } else if (e instanceof ConnectException
        || e instanceof UnknownHostException) {   //   连接错误
      onException(ExceptionReason.CONNECT_ERROR);
    } else if (e instanceof InterruptedIOException) {   //  连接超时
      onException(ExceptionReason.CONNECT_TIMEOUT);
    } else if (e instanceof JsonParseException
        || e instanceof JSONException
        || e instanceof ParseException) {   //  解析错误
      onException(ExceptionReason.PARSE_ERROR);
    } else if (e instanceof ServerResponseException) {
      onFail(e.getMessage());
    } else if (e instanceof NoDataExceptionException) {
      onSuccess(null);
    } else {
      onException(ExceptionReason.UNKNOWN_ERROR);
    }
    onFinish();
  }

  @Override
  public void onComplete() {
  }

  /**
   * 请求成功
   *
   * @param response 服务器返回的数据
   */
  abstract public void onSuccess(T response);

  /**
   * 服务器返回数据，但响应码不为200
   *
   */
  /**
   * 服务器返回数据，但响应码不为1000
   */
  public void onFail(String message) {
    ToastUtils.showShort(message);
  }

  public void onFinish() {
  }

  /**
   * 请求异常
   */
  public void onException(ExceptionReason reason) {
    switch (reason) {
      case CONNECT_ERROR:
        ToastUtils.showShort(R.string.connect_error, Toast.LENGTH_SHORT);
        break;

      case CONNECT_TIMEOUT:
        ToastUtils.showShort(R.string.connect_timeout, Toast.LENGTH_SHORT);
        break;

      case BAD_NETWORK:
        ToastUtils.showShort(R.string.bad_network, Toast.LENGTH_SHORT);
        break;

      case PARSE_ERROR:
        ToastUtils.showShort(R.string.parse_error, Toast.LENGTH_SHORT);
        break;

      case UNKNOWN_ERROR:
      default:
        ToastUtils.showShort(R.string.unknown_error, Toast.LENGTH_SHORT);
        break;
    }
  }

  /**
   * 请求网络失败原因
   */
  public enum ExceptionReason {
    /**
     * 解析数据失败
     */
    PARSE_ERROR,
    /**
     * 网络问题
     */
    BAD_NETWORK,
    /**
     * 连接错误
     */
    CONNECT_ERROR,
    /**
     * 连接超时
     */
    CONNECT_TIMEOUT,
    /**
     * 未知错误
     */
    UNKNOWN_ERROR,
  }
}
